package cn.com.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.xmlpull.v1.XmlPullParser;

import android.net.Uri;
import android.util.Xml;
import cn.com.domain.MsgEntity;
import cn.com.md5.MD5;

/**
 * 消息记录业务逻辑
 * @author ChonZY
 */
public class MsgService {
	/**
	 * 获取消息记录数据
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public List<MsgEntity> getMsgRecords(String path) throws Exception{
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
		if(conn.getResponseCode() == 200){
			InputStream is = conn.getInputStream();
			return parserXMLMsgRecords(is);
		}
		
		return null;
	}
	/**
	 * 解析消息记录 数据
	 * 解析流为消息记录的集合
	 * @param is
	 * @return
	 */
	private List<MsgEntity> parserXMLMsgRecords(InputStream is)throws Exception {
		List<MsgEntity> msgs = null ;
		MsgEntity msg = null ;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(is,"UTF-8");
		int eventType = parser.getEventType();
		
		while(eventType != XmlPullParser.END_DOCUMENT){
			switch (eventType) {
			case XmlPullParser.START_TAG:
				if("msgs".equals(parser.getName())){
					msgs = new ArrayList<MsgEntity>();
				}else if("msg".equals(parser.getName())){
					msg = new MsgEntity();
					int id = Integer.valueOf(parser.getAttributeName(0));
					msg.setId(id);
				}else if("src".equals(parser.getName())){
					String src = parser.nextText();
					msg.setSrc(src);
				}else if("num".equals(parser.getName())){
					String num = parser.nextText();
					msg.setNum(num);
				}else if("name".equals(parser.getName())){
					String name = parser.nextText();
					msg.setName(name);
				}else if("time".equals(parser.getName())){
					String time = parser.nextText();
					msg.setTime(time);
				}else if("content".equals(parser.getName())){
					String content = parser.nextText();
					msg.setContent(content);
				}
				break;

			case XmlPullParser.END_TAG:
				if("msgs".equals(parser.getName())){
					msgs.add(msg);
					msg = null ;
				}
				break ;
			}
			eventType = parser.next();
		}
		is.close();
		
		return msgs;
	}
	/**
	 * 获取图片的uri路径
	 * @param path 图片的服务器下载地址
	 * @param cacheFile
	 * @return
	 * @throws Exception
	 */
	public Uri getHeadImageUri(String path, File cacheFile) throws Exception{
		// 对图片的访问路径进行一个MD5 加密，把一个字符串变成32位的字符串
		String name = MD5.getMD5(path) + path.substring(path.lastIndexOf("."));
		
		File file  = new File(cacheFile,name);
		if(file.exists()){
			return Uri.fromFile(file); //文件存在 从缓存里面 拿
		}else{
			URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setReadTimeout(5000);
			conn.setRequestMethod("GET");
			if(conn.getResponseCode() == 200){
				InputStream is = conn.getInputStream();
				FileOutputStream fos = new FileOutputStream(file);
				byte [] buffer = new byte[1024];
				int len ;
				while((len = is.read(buffer)) != -1){
					fos.write(buffer,0,len);
				}
				fos.close();
				is.close();
				return Uri.fromFile(file);
			}
			
		}
		
		return null ;
	}

}
